/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.servlet;

import com.qdu.dao.Fenlei;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 39370
 */
@WebServlet("/fls")
public class FenleiServlet extends HttpServlet{
    public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession session=request.getSession();

		String name=(String)session.getAttribute("name");
		if(name==null){
			request.getRequestDispatcher("admin/login.jsp").forward(request, response);
		}
		else{
			String method=request.getParameter("method").trim();
			Fenlei fl=new Fenlei(); 
			if(method.equals("add")){
				String fenlei=request.getParameter("fenlei").trim();
				int i=fl.addFenLei(fenlei);
				if(i==1){
					request.setAttribute("message", "操作成功！");
					request.getRequestDispatcher("admin/fenlei.jsp").forward(request, response);
				}
				else if(i==3){
					request.setAttribute("message", "对不起，该用户名已经存在！");
					request.getRequestDispatcher("admin/addfenlei.jsp").forward(request, response);
				}
				else if(i==2){
					request.setAttribute("message", "系统维护中，请稍后再试！");
					request.getRequestDispatcher("admin/addfenlei.jsp").forward(request, response);
				}
			}
			else if(method.equals("del")){
				String str[]=request.getParameterValues("choose");
				if(str==null){
					request.setAttribute("message", "请选择操作对象！");
					request.getRequestDispatcher("admin/fenlei.jsp").forward(request, response);
				}
				else{
					int id= str.length;
					for(int i=0;i<str.length;i++){
						int s=Integer.parseInt(str[i]);				
						id=s;
					}
				
					int i=fl.delFenLei(id);
					if(i==1){
						request.setAttribute("message", "操作成功！");
						request.getRequestDispatcher("admin/fenlei.jsp").forward(request, response);
					}
					else{
						request.setAttribute("message", "系统维护中，请稍后再试！");
						request.getRequestDispatcher("admin/fenlei.jsp").forward(request, response);
					}
				}
			}
                }
    }
}
